<?php
class ParserController extends Controller
{

  public function actionPublicInfo()
  {
    $info = NULL;
    $formModel = new PublicInfoForm();
    if (isset($_POST['PublicInfoForm'])) {
      $formModel->attributes = $_POST['PublicInfoForm'];
      if ($formModel->validate()) {
        $info = Parser::parsePublicInfo($formModel->rawText);
        if (!empty($info)) {
          $existPlayer = Player::model()->loadPlayerByName($info['player']);
          if (!empty($existPlayer)) {
            /* In Funktion auslagern */
            $playerId = $existPlayer->id;
            $existPlayer->name = $info['player'];
            $existPlayer->alliance_short = $info['ally_short'];
            $existPlayer->alliance_long = $info['ally_long'];
            $existPlayer->meta_short = $info['meta_short'];
            $existPlayer->meta_long = $info['meta_long'];

            if ($existPlayer->save()) {
              Yii::log('Player '.$existPlayer->name.' aktualisiert', CLogger::LEVEL_TRACE, 'sntools.controller.publicInfo');
              // Status der "alte" Forschung aktualisieren
              Research::model()->updateAll(array('status'=>2),'player_id = :player_id AND status = 1', array(':player_id'=>$playerId));
              // Neue Forschungspunkte eintragen
              Research::model()->savePoints($playerId, $info['research']);
              // Status der "alten" Planeten aktualisieren
              Planet::model()->updateAll(array('status'=>2),'player_id = :player_id AND status = 1', array(':player_id'=>$playerId));
              Planet::model()->savePlanetsFromPublicInfo($playerId, $info['planets']);

              //$trans = Yii::app()->db->beginTransaction();
              $cmd = Yii::app()->db->createCommand();
              $planetDetailCnt = $cmd->update(PlanetDetail::model()->tableName(), array('status' => '2'), 'planet_id IN (
                             SELECT
                              id
                             FROM
                              '.Planet::model()->tableName().'
                             WHERE
                              player_id = :player_id)', array(':player_id' => $playerId));
              Yii::log($planetDetailCnt.' Planeten Details aktualisiert', CLogger::LEVEL_TRACE, 'sntools.controller.publicInfo');
              //$trans->rollback();
            }
          } else {
            $newPlayer = new Player();
            $newPlayer->name = $info['player'];
            $newPlayer->alliance_short = $info['ally_short'];
            $newPlayer->alliance_long = $info['ally_long'];
            $newPlayer->meta_short = $info['meta_short'];
            $newPlayer->meta_long = $info['meta_long'];
            if ($newPlayer->save()) {
              $newPlyId = Yii::app()->db->getLastInsertID();
              Yii::log('Player '.$newPlayer->name.' erstellt', CLogger::LEVEL_TRACE, 'sntools.controller.publicInfo');

              Research::model()->savePoints($newPlyId, $info['research']);
              Planet::model()->savePlanetsFromPublicInfo($newPlyId, $info['planets']);
            } // $newPlayer->save()
          } // kein Player gefunden
        } // !empty($info)
      } // $formModel->validate()
    } // isset($_POST['PublicInfoForm'])
    $this->render('publicInfo', array('model' => $formModel, 'info' => $info));
  }

}